package com.geekaca.ssm.service.impl;

import com.geekaca.ssm.dao.BrandMapper;
import com.geekaca.ssm.domain.Brand;
import com.geekaca.ssm.exception.BusinessException;
import com.geekaca.ssm.service.BrandService;
import com.geekaca.ssm.utils.Code;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandMapper brandMapper;

    @Override
    public List<Brand> getAll() {
        return brandMapper.selectAll();
    }

    @Override
    public List<Brand> getAllBrands(Brand brand, Integer pageNo, Integer pagesize) {
        int start = (pageNo-1);
        return brandMapper.getAllBrands(brand, start, pagesize);
    }

    @Override
    public int addBrand(Brand brand) {
        return brandMapper.insertBrand(brand);
    }

    @Override
    public int delBrand(Brand brand) {
        return brandMapper.deleteBrand(brand.getId());
    }

    @Override
    public int updateBrand(Brand brand) {
        return brandMapper.updateBrand(brand);
    }

    @Override
    public Brand selectById(Integer id) {
        return brandMapper.selectById(id);
    }

    @Override
    public Brand getByid(Integer id) {
        String date = "2023-09-04 15:32-00";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            LocalDateTime dateTime = LocalDateTime.parse(date, dtf);
        } catch (Exception e) {
            throw new BusinessException(Code.UPDATE_ERR, "日期格式不符合要求");
        }


        return brandMapper.selectById(id);
    }
}
